package com.chinafree.common.utils;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.region.Region;

import java.io.*;
import java.util.Properties;
import java.util.TreeMap;

public class TecentCos {

    private COSClient cosClient;

    private String bucketName = "ssl-1305984771";

    public TecentCos() {
        String secretId = "AKIDEJ8gxyjDQN3TbS3NQDk0ziBOKzThdIgn";
        String secretKey = "sTntjIZaWBahY8RTFkyvb1A1bvzmIXVB";

        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        Region region = new Region("ap-shanghai");
        ClientConfig clientConfig = new ClientConfig(region);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        cosClient = new COSClient(cred, clientConfig);
    }


    public InputStream getInputStream(String key) {
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        COSObject cosObject = cosClient.getObject(getObjectRequest);
        return cosObject.getObjectContent();
    }

    public byte[] getByte(String key) throws IOException {
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        COSObject cosObject = cosClient.getObject(getObjectRequest);
        return input2byte(cosObject.getObjectContent());

    }

    public static final byte[] input2byte(InputStream inStream)
            throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in2b = swapStream.toByteArray();
        return in2b;
    }


//    public void testGetCredential() {
//        TreeMap<String, Object> config = new TreeMap<String, Object>();
//
//        try {
//            Properties properties = new Properties();
//            File configFile = new File("local.properties");
//            properties.load(new FileInputStream(configFile));
//
//            // 云 api 密钥 SecretId
//            config.put("secretId", properties.getProperty("SecretId"));
//            // 云 api 密钥 SecretKey
//            config.put("secretKey", properties.getProperty("SecretKey"));
//
//            if (properties.containsKey("https.proxyHost")) {
//                System.setProperty("https.proxyHost", properties.getProperty("https.proxyHost"));
//                System.setProperty("https.proxyPort", properties.getProperty("https.proxyPort"));
//            }
//
//            // 设置域名
//            //config.put("host", "sts.internal.tencentcloudapi.com");
//
//            // 临时密钥有效时长，单位是秒
//            config.put("durationSeconds", 1800);
//
//            // 换成你的 bucket
//            config.put("bucket", "sts-sdk-test-1251668577");
//            // 换成 bucket 所在地区
//            config.put("region", "ap-chengdu");
//
//            // 可以通过 allowPrefixes 指定前缀数组, 例子： a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
//            config.put("allowPrefixes", new String[] {
//                    "exampleobject",
//                    "exampleobject2"
//            });
//
//            // 密钥的权限列表。简单上传和分片需要以下的权限，其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
//            String[] allowActions = new String[] {
//                    // 简单上传
//                    "name/cos:PutObject",
//                    "name/cos:PostObject",
//                    // 分片上传
//                    "name/cos:InitiateMultipartUpload",
//                    "name/cos:ListMultipartUploads",
//                    "name/cos:ListParts",
//                    "name/cos:UploadPart",
//                    "name/cos:CompleteMultipartUpload"
//            };
//            config.put("allowActions", allowActions);
//
//            Response response = CosStsClient.getCredential(config);
//            System.out.println(response.credentials.tmpSecretId);
//            System.out.println(response.credentials.tmpSecretKey);
//            System.out.println(response.credentials.sessionToken);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new IllegalArgumentException("no valid secret !");
//        }
//    }

}
